package com.ruoyi.web.controller.Dorm;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Dorm.domain.Dorm;
import com.ruoyi.Dorm.service.IDormService;
import com.ruoyi.School.domain.School;
import com.ruoyi.School.service.ISchoolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 宿舍 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-08-23
 */
@Controller
@RequestMapping("/Dorm/dorm")
public class DormController extends BaseController
{
    private String prefix = "Dorm/dorm";
	
	@Autowired
	private IDormService dormService;
	
	@Autowired
	private ISchoolService schoolService;
	
	@RequiresPermissions("Dorm:dorm:view")
	@GetMapping()
	public String dorm(ModelMap mmap)
	{
		School school=new School();
		List<School> schoolList = schoolService.selectSchoolList(school);
		mmap.put("schoolList", schoolList);		
	    return prefix + "/dorm";
	}
	
	/**
	 * 查询宿舍列表
	 */
	@RequiresPermissions("Dorm:dorm:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Dorm dorm)
	{
		startPage();
        List<Dorm> list = dormService.selectDormList(dorm);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出宿舍列表
	 */
	@RequiresPermissions("Dorm:dorm:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Dorm dorm)
    {
    	List<Dorm> list = dormService.selectDormList(dorm);
        ExcelUtil<Dorm> util = new ExcelUtil<Dorm>(Dorm.class);
        return util.exportExcel(list, "dorm");
    }
	
	/**
	 * 新增宿舍
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		School school=new School();
		List<School> schoolList = schoolService.selectSchoolList(school);
		mmap.put("schoolList", schoolList);	
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存宿舍
	 */
	@RequiresPermissions("Dorm:dorm:add")
	@Log(title = "宿舍", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Dorm dorm)
	{		
		School index=schoolService.selectSchoolById(dorm.getSchoolId());
		if(index==null){
			return AjaxResult.error("学区不可为空");
		}
		dorm.setCreateDate(DateTools.getYMDhms());
		String schoolName=index.getSchoolName();
		dorm.setSchoolName(schoolName);
		return toAjax(dormService.insertDorm(dorm));
	}

	/**
	 * 修改宿舍
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Dorm dorm = dormService.selectDormById(id);
		School school=new School();
		List<School> schoolList = schoolService.selectSchoolList(school);
		mmap.put("schoolList", schoolList);	
		mmap.put("dorm", dorm);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存宿舍
	 */
	@RequiresPermissions("Dorm:dorm:edit")
	@Log(title = "宿舍", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Dorm dorm)
	{		
		School index=schoolService.selectSchoolById(dorm.getSchoolId());
		if(index==null){
			return AjaxResult.error("学区不可为空");
		}
		dorm.setUpdateDate(DateTools.getYMDhms());
		String schoolName=index.getSchoolName();
		dorm.setSchoolName(schoolName);
		return toAjax(dormService.updateDorm(dorm));
	}
	
	/**
	 * 删除宿舍
	 */
	@RequiresPermissions("Dorm:dorm:remove")
	@Log(title = "宿舍", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(dormService.deleteDormByIds(ids));
	}
	
}
